layui.define(['form', 'layedit', 'laydate', 'element'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let element = layui.element;
    let relateId = obj.baseCompanyArea.relateId;
    let index;

    form.on('submit(zmn-skillCateg-form-submit)', function (obj) {
        zmnButtonUtil.disabledButton("submit", $);
        //拿到 id为contents下class 属性有  layui-tab-item 的子集div
        let children = $("#contents").children(".layui-tab-item");
        let array = [];
        children.each(function (index, val) {
            let servCategId, categOneId;
            //将服务分类进行保存
            let input = $(val).children("input:hidden");
            if (zmnUtil.isNotNull(input.val())) {
                // map.set("sevType", input.val());
                servCategId = input.val();
            }
            //找到 每个tab 下承载 复选框的 div
            let formItem = $(val).children(".layui-form-item");
            formItem.each(function (i, vaal) {
                let zmn = $(vaal).find(".zmn");
                categOneId = zmn.val();
                let find1 = $(vaal).find("input:checkbox:checked");
                if (find1.length > 0) {
                    find1.each(function (indexa, vals) {
                        if (!$(vals).is(".checkMe")) {
                            let baseCompanyCategory = {};
                            baseCompanyCategory["relateId"] = relateId;
                            baseCompanyCategory["servCategId"] = servCategId;
                            baseCompanyCategory["categOneId"] = categOneId;
                            baseCompanyCategory["categId"] = $(vals).val();
                            array.push(baseCompanyCategory);
                        }
                    });
                }
            });
        });
        let url = array.length == 0 ? "/company/area/deleteByRelateId.action?relateId="+relateId : "/company/area/baseCompanyCategory.action";
        let data = array.length == 0 ? {} : JSON.stringify(array);
        $.ajax({
            async: true,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: data,
            beforeSend: function () {
                layLoding();
            },
            success: function (json) {
                layer.close(index);
                if (json.status == 200) {
                    zmnLayUtil.closeThisIframeMsg(parent, json.message);
                    parent.reload(json.message);
                }
                layer.msg(json.message);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
        zmnButtonUtil.disabledButton("submit", $);
        return false;
    });

    loadSevType(obj.servCategList, element);
    laodChecked(obj.companyCategoryList);
    loadSecuritylectAllNode();

    function loadSevType(val, element) {
        layLoding();
        //根据数据渲染服务分类，产品分类
        if (zmnUtil.isNull(val)) {
            return false;
        }
        for (let i = 0; i < val.length; i++) {
            //判断等于0 的tab 页 将其选中渲染
            if (i == 0) {
                $("#sevType").append('<li class="layui-this">' + val[i].name + '</li>');
                $("#contents").append('<div class="layui-tab-item layui-show"><input type="hidden" value="' + val[i].categId + '">' + loadProductCateg(val[i].categId) + '<div class="layui-split">&nbsp;</div></div>');
            } else {
                //渲染tab 页 样式
                $("#sevType").append('<li>' + val[i].name + '</li>');
                //渲染多选框样式
                $("#contents").append('<div class="layui-tab-item"><input type="hidden" value="' + val[i].categId + '">' + loadProductCateg(val[i].categId) + '<div class="layui-split">&nbsp;</div></div>');
            }
        }
        element.init();
        form.render(null, "zmn-skillCateg-form");
    }

    function loadProductCateg(val) {
        //渲染一级分类数据
        let categList = obj.categList;

        if (zmnUtil.isNull(categList)) {
            return "";
        }
        let produHtml = "";
        for (let i = 0; i < categList.length; i++) {
            if (categList[i].vtClassify != null) {
                if (categList[i].vtClassify.pid == val) {
                    produHtml += '<div class="layui-form-item" pane=""><input class="zmn" type="hidden" value="' + categList[i].vtClassify.value + '" /><label class="layui-form-label  positioning">' + categList[i].vtClassify.text + ':</label>' +
                        '<div class="layui-input-block">' + formatterCheckboxValue(categList[i].secondClassifyList, categList[i].vtClassify.value) + '</div></div>';
                }
            }
        }
        return produHtml;
    }

    //渲染二级分类数据
    function formatterCheckboxValue(val, pid) {

        let checkboxHtml = "";

        for (let i = 0; i < val.length; i++) {

            if (val[i].value != pid) {
                checkboxHtml += '<input type="checkbox" class="secondary" lay-filter="secondary"  lay-skin="primary" value="' + val[i].value + '" title="' + val[i].text + '" >'
            }
        }
        return checkboxHtml;
    }

    function laodChecked(companyCategoryList) {
        //反选复选框 第一步判断 relateId 是否符合
        if (companyCategoryList.length > 0 && companyCategoryList[0].relateId == relateId) {
            //拿到 id为contents下class 属性有  layui-tab-item 的子集div
            let children = $("#contents").children(".layui-tab-item");
            children.each(function (index, val) {
                //将服务分类进行保存
                let input = $(val).children("input:hidden");
                for (let i = 0; i < companyCategoryList.length; i++) {
                    if (companyCategoryList[i].servCategId == input.val()) {
                        let formItem = $(val).children(".layui-form-item");
                        formItem.each(function (index, vaal) {
                            //将一级分类的数据保存
                            let zmn = $(vaal).find(".zmn");
                            if (companyCategoryList[i].categOneId == zmn.val()) {
                                let find1 = $(vaal).find("input:checkbox");
                                find1.each(function (index, vasssl) {
                                    if ($(vasssl).val() == companyCategoryList[i].categId) {
                                        $(vasssl).attr("checked", "checked");
                                    }
                                })
                            }
                        });
                    }
                }
            });
        }
        form.render(null, "zmn-skillCateg-form");
    }

    function loadSecuritylectAllNode() {
        $(".positioning").toArray().forEach(e => {
            $(e).next().prepend('<input type="checkbox" class="checkMe" lay-skin="primary" value="1" title="全选" lay-filter="checkMe" >');
            let status = false;
            $(e).next().find("input:checkbox").toArray().forEach(n => {
                if (!$(n).prop("checked") && $(n).attr("class") == "secondary") {
                    $(n).closest(".layui-input-block").find(".checkMe").prop("checked", false);
                    status = true;
                } else {
                    if (status) {
                        $(n).closest(".layui-input-block").find(".checkMe").prop("checked", false);
                    } else {
                        $(n).closest(".layui-input-block").find(".checkMe").prop("checked", true);
                    }
                }
            });
        });
        form.render(null, "zmn-skillCateg-form");
        layer.close(index);
    }

    //监听指定开关
    form.on('checkbox(checkMe)', function (data) {
        if (data.elem.checked) {
            $(data.elem).closest(".layui-input-block").find("input:checkbox").toArray().forEach(e => {
                $(e).prop("checked", true);
            });
        } else {
            $(data.elem).closest(".layui-input-block").find("input:checkbox").toArray().forEach(e => {
                $(e).prop("checked", false);
            });
        }

        form.render(null, "zmn-skillCateg-form");
    });
    form.on('checkbox(secondary)', function (data) {
        if (!data.elem.checked) {
            $(data.elem).closest(".layui-input-block").find(".checkMe").prop("checked", false);
        } else {
            let list = $(data.elem).closest(".layui-input-block").find("input:checkbox").toArray();
            list.forEach(e => {
                if (!$(e).prop("checked")) {
                    $(e).closest(".layui-input-block").find(".checkMe").prop("checked", false);
                } else {
                    $(e).closest(".layui-input-block").find(".checkMe").prop("checked", true);
                }
            });
            let checedList = $(data.elem).closest(".layui-input-block").find("input:checkbox:checked").toArray();

            if (list.length != checedList.length) {
                $(e).closest(".layui-input-block").find(".checkMe").prop("checked", false);
            }

        }
        form.render(null, "zmn-skillCateg-form");
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        zmnLayUtil.closeThisIframe(parent);
    });

    function layLoding() {
        index = layer.load(1, {
            shade: [0.5, '#DBDBDB'] //0.1透明度的白色背景
        });
    }

    exports('company_category_edit', {});
});


